
 



 
 
 



 
 
 <!DOCTYPE html>
<html>
<head>
 <link rel="icon" type="image/vnd.microsoft.icon" href="http://www.gstatic.com/codesite/ph/images/phosting.ico">
 
 <script type="text/javascript">
 
 
 
 
 var codesite_token = null;
 
 
 var CS_env = {"token":null,"assetHostPath":"http://www.gstatic.com/codesite/ph","domainName":null,"assetVersionPath":"http://www.gstatic.com/codesite/ph/9104208085135278598","projectName":"linqtoexcel","projectHomeUrl":"/p/linqtoexcel","absoluteBaseUrl":"http://code.google.com","relativeBaseUrl":"","urlPrefix":"p","loggedInUserEmail":null};
 </script>
 
 
 <title>UsingLinqToExcel - 
 linqtoexcel -
 
 This page will help you start using the Linq to Excel library in your project - 
 A LINQ Provider - Google Project Hosting
 </title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" >
 
 <meta name="ROBOTS" content="NOARCHIVE">
 
 <link type="text/css" rel="stylesheet" href="http://www.gstatic.com/codesite/ph/9104208085135278598/css/ph_core.css">
 
 <link type="text/css" rel="stylesheet" href="http://www.gstatic.com/codesite/ph/9104208085135278598/css/ph_detail.css" >
 
 
 
 <link type="application/atom+xml" rel="alternate" href="/feeds/p/linqtoexcel/svnchanges/basic?path=/wiki/UsingLinqToExcel.wiki">
 
 
<!--[if IE]>
 <link type="text/css" rel="stylesheet" href="http://www.gstatic.com/codesite/ph/9104208085135278598/css/d_ie.css" >
<![endif]-->
 <style type="text/css">
 .menuIcon.off { background: no-repeat url(http://www.gstatic.com/codesite/ph/images/dropdown_sprite.gif) 0 -42px }
 .menuIcon.on { background: no-repeat url(http://www.gstatic.com/codesite/ph/images/dropdown_sprite.gif) 0 -28px }
 .menuIcon.down { background: no-repeat url(http://www.gstatic.com/codesite/ph/images/dropdown_sprite.gif) 0 0; }
 
 
 #maincol {
 padding-top: 0;
 padding-bottom: 0;
 }

 </style>
</head>
<body class="t6">
 <script type="text/javascript">
 var _gaq = _gaq || [];
 _gaq.push(
 ['siteTracker._setAccount', 'UA-18071-1'],
 ['siteTracker._trackPageview']);
 
 _gaq.push(
 ['projectTracker._setAccount', 'UA-6302798-1'],
 ['projectTracker._trackPageview']);
 
 (function() {
 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
 (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
 })();
 </script>
<div class="headbg">
 <div id="gaia">
 

 <span>
 
 <a href="#" id="projects-dropdown" onclick="return false;"><u>My favorites</u> <small>&#9660;</small></a>
 | <a href="https://www.google.com/accounts/ServiceLogin?service=code&amp;ltmpl=phosting&amp;continue=http%3A%2F%2Fcode.google.com%2Fp%2Flinqtoexcel%2Fwiki%2FUsingLinqToExcel&amp;followup=http%3A%2F%2Fcode.google.com%2Fp%2Flinqtoexcel%2Fwiki%2FUsingLinqToExcel" onclick="_CS_click('/gb/ph/signin');"><u>Sign in</u></a>
 
 </span>

 </div>
 <div class="gbh" style="left: 0pt;"></div>
 <div class="gbh" style="right: 0pt;"></div>
 
 
 <div style="height: 1px"></div>
<!--[if lte IE 7]>
<div style="text-align:center;">
Your version of Internet Explorer is not supported. Try a browser that
contributes to open source, such as <a href="http://www.firefox.com">Firefox</a>,
<a href="http://www.google.com/chrome">Google Chrome</a>, or
<a href="http://code.google.com/chrome/chromeframe/">Google Chrome Frame</a>.
</div>
<![endif]-->




 <table style="padding:0px; margin: 0px 0px 10px 0px; width:100%" cellpadding="0" cellspacing="0">
 <tr style="height: 58px;">
 
 <td id="plogo">
 <a href="/p/linqtoexcel/">
 
 <img src="http://www.gstatic.com/codesite/ph/images/defaultlogo.png" alt="Logo">
 
 </a>
 </td>
 
 <td style="padding-left: 0.5em">
 
 <div id="pname">
 <a href="/p/linqtoexcel/">linqtoexcel</a>
 </div>
 
 <div id="psum">
 <a id="project_summary_link" href="/p/linqtoexcel/" >A LINQ Provider</a>
 
 </div>
 
 
 </td>
 <td style="white-space:nowrap;text-align:right; vertical-align:bottom;">
 
 <form action="/hosting/search">
 <input size="30" name="q" value="" type="text">
 <input type="submit" name="projectsearch" value="Search projects" >
 </form>
 
 </tr>
 </table>

</div>

 
<div id="mt" class="gtb"> 
 <a href="/p/linqtoexcel/" class="tab ">Project&nbsp;Home</a>
 
 
 
 
 <a href="/p/linqtoexcel/downloads/list" class="tab ">Downloads</a>
 
 
 
 
 
 <a href="/p/linqtoexcel/w/list" class="tab active">Wiki</a>
 
 
 
 
 
 <a href="/p/linqtoexcel/issues/list"
 class="tab ">Issues</a>
 
 
 
 
 
 <a href="/p/linqtoexcel/source/checkout"
 class="tab ">Source</a>
 
 
 
 
 
 <div class=gtbc></div>
</div>
<table cellspacing="0" cellpadding="0" width="100%" align="center" border="0" class="st">
 <tr>
 
 
 
 <td class="subt">
 <div class="issueDetail">
<div class="isf">
 
 <span class="inIssueList"> 
 <span>Search</span>
 <form action="/p/linqtoexcel/w/list" method="GET" style="display:inline">
 <select id="can" name="can" >
 <option disabled="disabled">Search within:</option>
 
 <option value="1" >&nbsp;All wiki pages</option>
 <option value="3" >&nbsp;Featured pages</option>
 <option value="2" selected="selected">&nbsp;Current pages</option>
 
 
 <option value="4" >&nbsp;Deprecated pages</option>
 
 </select>
 <span>for</span>
 <span id="qq"><input type="text" size="38" id="searchq" name="q" value=""
 autocomplete="off" ></span>
 
 
 <input type="submit" value="Search" >
 </form>
 </span>

 
 
 
 
 
 
 

</div>
</div>

 </td>
 
 
 
 
 
 
 <td align="right" valign="top" class="bevel-right"></td>
 </tr>
</table>


<script type="text/javascript">
 var cancelBubble = false;
 function _go(url) { document.location = url; }
</script>
<div id="maincol"
 
>

 
<!-- IE -->







 <style type="text/css">
 .delcom { background: #e8e8e8 }
 .commentcontent {
 margin: 2em;
 padding: 0px 10px;
 width: 66em;
 }
 .artifactcomment {
 border-top: 3px solid #c3d9ff;
 }
 #commentform {
 border-top: 3px solid #c3d9ff;
 }
 </style>

<div id="wikipage">
<table>
 <tr>
 
 
 <td style="vertical-align:top; padding-left:5px">
 
 <div id="wikiheader">
 
 <span style="font-size:120%;font-weight:bold">UsingLinqToExcel</span>
 &nbsp;
 <div> 
 
 <i>This page will help you start using the Linq to Excel library in your project</i>
 
 
 <br/>
 
 <a class="label" style="padding-top: 2px"
 href="/p/linqtoexcel/w/list?q=label:Featured"
 title="Listed on project home page"
 >Featured</a>
 
 
 
 <div id="wikiauthor" style="float:right">
 Updated <span title="Fri Apr 22 08:32:59 2011">
 Apr 22, 2011</span>
 
 by <a style="white-space: nowrap" href="/u/@VxVQQ1xUAxNCXAl7/">paulyo...@gmail.com</a>
 
 </div>
 </div>
 </div>
 
 <div id="wikicontent">
 <div class="vt" id="wikimaincol">
 <h2><a name="Adding_to_your_project"></a>Adding LinqToExcel to your project<a href="#Adding_to_your_project" class="section_anchor"></a></h2><h4><a name=""></a>NuGet</h4><p>You can use NuGet to quickly add LinqToExcel to your project. Just search for &#x27;linqtoexcel&#x27; and install the package. </p><h4><a name="Manually_Add_References"></a>Manually Add References<a href="#Manually_Add_References" class="section_anchor"></a></h4><p><a href="http://code.google.com/p/linqtoexcel/downloads/list" rel="nofollow">Download</a> the latest files and add the following references in your project <ul><li><strong>LinqToExcel.dll</strong> </li><li><strong>Remotion.Data.Linq.dll</strong> </li></ul>   </p><h4><a name="Target_x86_Platform"></a>Target x86 Platform<a href="#Target_x86_Platform" class="section_anchor"></a></h4><p>Linq to Excel requires any projects referencing it to be built against the x86 platform target. See <a href="http://blog.yodersolutions.com/2008/11/microsoftjetoledb-and-x64-machines/" rel="nofollow">this link</a> for detailed information on setting the platform target to x86. Note this only applies to compiling the project on x64 computers. </p><h4><a name=".Net_4"></a>.Net 4<a href="#.Net_4" class="section_anchor"></a></h4><p>When using Linq to Excel in a .Net 4 app, make sure to change the target framework from the default client profile to the full .Net 4 framework. (Properties -&gt; Application -&gt; Target framework) The client profile cannot compile .Net 3.5 dlls. </p><p><br></br> </p><h2><a name="Query_a_worksheet_with_a_header_row"></a>Query a worksheet with a header row<a href="#Query_a_worksheet_with_a_header_row" class="section_anchor"></a></h2><p>The default query expects the first row to be the header row containing column names that match the property names on the generic class being used. It also expects the data to be in the worksheet named &quot;Sheet1&quot;. </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
var indianaCompanies = from c in excel.Worksheet&lt;Company&gt;()
                       where c.State == &quot;IN&quot;
                       select c;</pre><p><br></br> </p><h2><a name="Query_a_specific_worksheet_by_name"></a>Query a specific worksheet by name<a href="#Query_a_specific_worksheet_by_name" class="section_anchor"></a></h2><p>Data from the worksheet named &quot;Sheet1&quot; is queried by default. To query a worksheet with a different name, pass the worksheet name in as an argument. </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
var oldCompanies = from c in repo.Worksheet&lt;Company&gt;(&quot;US Companies&quot;) //worksheet name = &#x27;US Companies&#x27;
                   where c.LaunchDate &lt; new DateTime(1900, 0, 0)
                   select c;</pre><p><br></br> </p><h2><a name="Property_to_column_mapping"></a>Property to column mapping<a href="#Property_to_column_mapping" class="section_anchor"></a></h2><p>Column names from the worksheet can be mapped to specific property names on the class by using the <strong>AddMapping&lt;&gt;()</strong> method. </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
excel.AddMapping&lt;Company&gt;(x =&gt; x.State, &quot;Providence&quot;); //maps the &quot;State&quot; property to the &quot;Providence&quot; column

var indianaCompanies = from c in excel.Worksheet&lt;Company&gt;()
                       where c.State == &quot;IN&quot; &amp;&amp; c.Employees &gt; 500
                       select c;</pre><p><br></br> </p><h2><a name="Using_the_.Row_class"></a>Using the LinqToExcel.Row class<a href="#Using_the_.Row_class" class="section_anchor"></a></h2><p>Query results can be returned as LinqToExcel.Row objects which allows you to access a cell&#x27;s value by using the column name in the string index. Just use the <strong>Worksheet()</strong> method without a generic argument. </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
var indianaCompanies = from c in excel.Worksheet()
                       where c[&quot;State&quot;] == &quot;IN&quot; || c.Zip == 46550
                       select c;</pre><p>The LinqToExcel.Row class allows you to easily cast a cell&#x27;s value by using its <strong>Cast&lt;&gt;()</strong> method </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
var largeCompanies = from c in excel.Worksheet()
                     where c[&quot;EmployeeCount&quot;].Cast&lt;int&gt;() &gt; 500
                     select c;</pre><p><br></br> </p><h2><a name="Query_a_worksheet_without_a_header_row"></a>Query a worksheet without a header row<a href="#Query_a_worksheet_without_a_header_row" class="section_anchor"></a></h2><p>Worksheets that do not contain a header row can also be queried by using the <strong>WorksheetNoHeader()</strong> method. The cell values are referenced by index. </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
var indianaCompanies = from c in excel.WorksheetNoHeader()
                       where c[2] == &quot;IN&quot; //value in 3rd column
                       select c;</pre><p><br></br> </p><h2><a name="Query_a_specific_range_within_a_worksheet"></a>Query a specific range within a worksheet<a href="#Query_a_specific_range_within_a_worksheet" class="section_anchor"></a></h2><p>Data from only specific cell ranges within a worksheet can be queried as well. </p><p>If the range contains a header row, then use the <strong>WorksheetRange()</strong> method </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
var indianaCompanies = from c in excel.WorksheetRange&lt;Company&gt;(&quot;B3&quot;, &quot;G10&quot;) //Selects data within the B3 to G10 cell range
                       where c.State == &quot;IN&quot;
                       select c;</pre><p>If the range does not contain a header row, then use the <strong>WorksheetRangeNoHeader()</strong> method </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
var indianaCompanies = from c in excel.WorksheetRangeNoHeader(&quot;B3&quot;, &quot;G10&quot;) //Selects data within the B3 to G10 cell range
                       where c[2] == &quot;IN&quot; //value in 3rd column (D column in this case)
                       select c;</pre><p><br></br> </p><h2><a name="Query_a_specific_worksheet_by_index"></a>Query a specific worksheet by index<a href="#Query_a_specific_worksheet_by_index" class="section_anchor"></a></h2><p>A specific worksheet can be queried by its index in relation to the other worksheets in the spreadsheet.  </p><p>The worsheets index order is based on their names alphatically; not the order they appear in Excel. For example, if a spreadsheet contains 2 worksheets: &quot;ten&quot; and &quot;eleven&quot;. Although &quot;eleven&quot; is the second worksheet in Excel, it is actually the first index. </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
var oldCompanies = from c in repo.Worksheet&lt;Company&gt;(1) //Queries the second worksheet in alphabetical order
                   where c.LaunchDate &lt; new DateTime(1900, 0, 0)
                   select c;</pre><p><br></br> </p><h2><a name="Apply_transformations"></a>Apply transformations<a href="#Apply_transformations" class="section_anchor"></a></h2><p>Transformations can be applied to cell values before they are set on the class properties. The example below transforms &quot;Y&quot; values in the &quot;IsBankrupt&quot; column to a boolean value of true. </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
excel.AddTransformation&lt;Company&gt;(x =&gt; x.IsBankrupt, cellValue =&gt; cellValue == &quot;Y&quot;);

var bankruptCompanies = from c in excel.Worksheet&lt;Company&gt;()
                        where c.IsBankrupt == true
                        select c;</pre><p><br></br> </p><h2><a name="Query_CSV_files"></a>Query CSV files<a href="#Query_CSV_files" class="section_anchor"></a></h2><p>Data from CSV files can be queried the same way spreadsheets are queried. </p><pre class="prettyprint">var csv = new ExcelQueryFactory(&quot;csvFileName&quot;);
var indianaCompanies = from c in csv.Worksheet&lt;Company&gt;()
                       where c.State == &quot;IN&quot;
                       select c;</pre><h3><a name="Query_Worksheet_Names"></a>Query Worksheet Names<a href="#Query_Worksheet_Names" class="section_anchor"></a></h3><p>The <strong>GetWorksheetNames()</strong> method can be used to retrieve the list of worksheet names in a spreadsheet. </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
var worksheetNames = excel.GetWorksheetNames();</pre><h3><a name="Query_Column_Names"></a>Query Column Names<a href="#Query_Column_Names" class="section_anchor"></a></h3><p>The <strong>GetColumnNames()</strong> method can be used to retrieve the list of column names in a worksheet. </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
var columnNames = excel.GetColumnNames(&quot;worksheetName&quot;);</pre><h3><a name="Strict_Mapping"></a>Strict Mapping<a href="#Strict_Mapping" class="section_anchor"></a></h3><p>Set the <strong>StrictMapping</strong> property to true to confirm all the columns in the worksheet map to a property on the class being used. A <strong>StrictMappingException</strong> is thrown when not all the columns map to a class property. </p><pre class="prettyprint">var excel = new ExcelQueryFactory(&quot;excelFileName&quot;);
excel.StrictMapping = true;</pre>
 </div>
 </div>
 </td><tr>
</table>
 </div>


 



 
 <br>
 <div class="artifactcomment">
 <span class="indicator">&#9658;</span> <a href="https://www.google.com/accounts/ServiceLogin?service=code&amp;ltmpl=phosting&amp;continue=http%3A%2F%2Fcode.google.com%2Fp%2Flinqtoexcel%2Fwiki%2FUsingLinqToExcel&amp;followup=http%3A%2F%2Fcode.google.com%2Fp%2Flinqtoexcel%2Fwiki%2FUsingLinqToExcel"
 >Sign in</a> to add a comment
 </div>
 
 
<form name="delcom" action="../w/delComment.do" method="POST">
 <input type="hidden" name="sequence_num" value="" >
 <input type="hidden" name="create_time" value="" >
 <input type="hidden" name="mode" value="" >
 <input type="hidden" name="pagename" value="UsingLinqToExcel" >
 <input type="hidden" name="token" value="" >
</form>



 <script src="http://www.gstatic.com/codesite/ph/9104208085135278598/js/prettify/prettify.js"></script>
 <script type="text/javascript">
 prettyPrint();
 </script>

<script type="text/javascript" src="http://www.gstatic.com/codesite/ph/9104208085135278598/js/dit_scripts.js"></script>



  
 
 
 <script type="text/javascript" src="http://www.gstatic.com/codesite/ph/9104208085135278598/js/ph_core.js"></script>
 
 
 
 
 <script type="text/javascript" src="/js/codesite_product_dictionary_ph.pack.04102009.js"></script>
</div> 
<div id="footer" dir="ltr">
 <div class="text">
 &copy;2011 Google -
 <a href="/projecthosting/terms.html">Terms</a> -
 <a href="http://www.google.com/privacy.html">Privacy</a> -
 <a href="/p/support/">Project Hosting Help</a>
 </div>
</div>
 <div class="hostedBy" style="margin-top: -20px;">
 <span style="vertical-align: top;">Powered by <a href="http://code.google.com/projecthosting/">Google Project Hosting</a></span>
 </div>
 
 


 
 </body>
</html>


